import org.I0Itec.zkclient.ZkClient;
import sun.management.snmp.jvminstr.JvmOSImpl;

public class Publisher {

    ZkClient zkClient = null;
    public static final String server = "linux01:2181,linux02:2181,linux03:2181";
    public static final String path = "/zktask/dblink";

    /**
     * 获取zk连接对象
     */
    public void connectZk() {
        zkClient = new ZkClient(server);

        //判断是否存在path节点，不存在则创建该节点
        if (!zkClient.exists(path)) {
            zkClient.createPersistent(path, true);
        }
    }

    /**
     * 发布数据库连接信息
     *
     * @param info 连接信息
     */
    public void publish(String info) {
        connectZk();
        zkClient.writeData(path, info);
        System.out.println("成功发布数据库连接信息：");
        System.out.println((String) zkClient.readData(path));
    }

    //删除节点
    public void delete () {
        zkClient.deleteRecursive(path);
    }

}
